package com.mashibing.juc.c_000;

/**
 * @author: Yuanzt
 * @Date: 2021/3/24
 */
public class T03_Sleep_Yield_Join {

    public static void main(String[] args) {
//        testSleep();
//        testYield();
        testJoin();
    }

    static void testSleep(){
        new Thread(()->{
            for (int i=0;i<10;i++){
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //让出当前CPU资源，回到等待队列（让出一下CPU，当前线程也在等待队列中抢CPU资源）
    static void testYield(){
        new Thread(()->{
            for (int i=0;i<10;i++){
                System.out.println("-------------B" + i);
                if (i%10 == 0) Thread.yield();
            }
        }).start();
    }

    static void testJoin(){
        Thread t1 = new Thread(()->{
            for (int i=0;i<10;i++){
                System.out.println("Thread :t1(" + i+")");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(()->{
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread: t2");
        });
        t2.start();
        t1.start();
    }
}
